package com.yeeft.ocr.service;

import com.baidu.aip.ocr.AipOcr;
import com.yeeft.ocr.domain.model.response.OcrResponse;
import com.yeeft.ocr.domain.props.OcrBaiduProps;
import com.yeeft.ocr.domain.vo.CompanyLicense;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 功能简要描述
 *
 * @author 赵仲文
 * @Copyright (c) 2016, frontpay.cn
 * @date 2018/5/9 9:57
 */
@Slf4j
@Data
@Service
public class CompanyLienseService {
    @Autowired
    private OcrBaiduProps ocrBaiduProps;

    public OcrResponse<CompanyLicense> getCompanyLicense() {

            // 初始化一个AipOcr
            AipOcr client = new AipOcr(ocrBaiduProps.getAppId(), ocrBaiduProps.getAppKey(), ocrBaiduProps.getSecretKey());

            // 可选：设置网络连接参数
            client.setConnectionTimeoutInMillis(2000);
            client.setSocketTimeoutInMillis(60000);

            // 调用接口
            String path = "test.jpg";
            JSONObject res = client.businessLicense(path, new HashMap<String, String>());
            log.info(res.toString(2));

            OcrResponse<CompanyLicense> data = new OcrResponse<>();
            data.setLogId(res.getString("logId"));
            data.setSuccess(res.getBoolean("success"));
            CompanyLicense companyLicense = new CompanyLicense();
            companyLicense.setLicenseNo(res.getString("reg_num"));
            companyLicense.setName(res.getString("name"));
            data.setData(companyLicense);

            return data;
    }
}
